package fy.test;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.seeyon.client.CTPRestClient;
import com.seeyon.client.CTPServiceClientManager;

import fy.form.listen.bean.FormBean;
import fy.form.listen.operate.IFormOperator;

/**
 * 存储表单各个输入项
 * 
 * @author Administrator
 * 
 */
abstract class AForm_Save_Operator_Rest implements IFormOperator {

	protected FormBean formBean;

	// protected PersonExport createPerson;

	protected short flowStatus;

	protected String templeteCode;

	protected boolean success;

	public void setFlowStatus(short flowStatus) {
		this.flowStatus = flowStatus;
	}

	public void setTempleteCode(String templeteCode) {
		this.templeteCode = templeteCode;
	}

	public void operate(long summaryID, long memberID) throws Exception {

		formBean = new FormBean();

		formBean.setSummaryId(summaryID);
		formBean.setOperatorID(memberID);

		parse();

		success = save();
	}

	public boolean isSuccess() {
		return success;
	}

	/**
	 * 获取需要的表单数据
	 * 
	 * @throws Exception
	 */
	protected void parse() throws Exception {

		// 指定协议、IP和端口，获取ClientManager
		CTPServiceClientManager clientManager = CTPServiceClientManager
				.getInstance("http://10.111.0.54:80");

		// 取得REST动态客户机实例
		CTPRestClient client = clientManager.getRestClient();
		client.authenticate("fy_form_reader", "form_reader2015");
		String result = client.get("flow/data/" + formBean.getSummaryId(),
				String.class);
		System.out.println(formBean.getSummaryId());
		Document doc = DocumentHelper.parseText(result);

		List list = doc.selectNodes("//values//column");

		for (int i = 0; i < list.size(); i++) {
			Element e = (Element) list.get(i);
			String name = e.attributeValue("name");
			String value = ((Element) e.elements().get(0)).getText()
					.replace("<![CDATA[", "").replace("]]>", "");

			parseValueExport(name, value);
		}

	}

	/**
	 * 获取字段数据
	 * 
	 * @param ve
	 */
	protected abstract void parseValueExport(String name, String value);

	protected abstract boolean save();
}
